package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.MerchantPayRegLog;
import com.tiancheng.onecode.enums.MerchantRegStatusEnums;
import com.tiancheng.onecode.mapper.MerchantPayRegLogMapper;
import com.tiancheng.onecode.service.IMerchantPayRegLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 商户进件日志
 */
@Service
public class MerchantPayRegLogServiceImpl extends ServiceImpl<MerchantPayRegLogMapper, MerchantPayRegLog> implements IMerchantPayRegLogService {

    @Override
    public void saveFlowLog(Long merchantPayRegId, MerchantRegStatusEnums currentFlow, MerchantRegStatusEnums preFlow, int flowStatus) {
        /*this.list(new LambdaQueryWrapper<MerchantPayRegLog>().eq(MerchantPayRegLog::getMerchantPayRegId,merchantPayRegId)
                .eq(MerchantPayRegLog::getCurrentFlow,currentFlow.getCode()));*/
        MerchantPayRegLog merchantPayRegLog = new MerchantPayRegLog();
        merchantPayRegLog.setMerchantPayRegId(merchantPayRegId);
        merchantPayRegLog.setCurrentFlow(currentFlow.getCode());
        merchantPayRegLog.setCurrentFlowDescribe(currentFlow.getName());
        merchantPayRegLog.setPreFlow(preFlow.getCode());
        merchantPayRegLog.setPreFlowDescribe(preFlow.getName());
        merchantPayRegLog.setFlowStatus(flowStatus);
        if(flowStatus==1||flowStatus==2){
            merchantPayRegLog.setLastUpdDt(LocalDateTime.now());
        }
        merchantPayRegLog.setCreatedDt(LocalDateTime.now());
        this.save(merchantPayRegLog);
        updateFlowStatus(merchantPayRegId,preFlow,1);
    }


    /**
     * 更新节点状态:<br/>
     * 0-进行中，1-成功，2-失败
     */
    private void updateFlowStatus(Long merchantPayRegId,MerchantRegStatusEnums flow,int flowStatus){
        MerchantPayRegLog updateEntity = new MerchantPayRegLog();
        updateEntity.setFlowStatus(flowStatus);
        updateEntity.setLastUpdDt(LocalDateTime.now());
        this.update(updateEntity,new LambdaQueryWrapper<MerchantPayRegLog>()
                .eq(MerchantPayRegLog::getMerchantPayRegId,merchantPayRegId).eq(MerchantPayRegLog::getFlowStatus,0)
                .eq(MerchantPayRegLog::getCurrentFlow,flow.getCode()));
    }
}
